var cgdContent = {
    //填充合同内容

    fillData: function () {
        fillHtmxByCgd();
        fillFyByCgd();
    }
};

$(function () {
//    cgdContent.fillHtmxByHt($("input[name='cgd.id']").val());

    cgdContent.fillData();
})

function fillHtmxByCgd() {
    fillHtmx($.webPath() + '/cggl/cgdtz/cgdgl_cgmx?cgd.id=' + $("[name='cgd.id']").val());
}

function fillFyByCgd() {
    fillFy($.webPath() + '/cggl/cgdtz/cgdgl_cgfy?cgd.id=' + $("[name='cgd.id']").val());
}

function fillHtmx(href) {
    var column;
    if ($("#cgdtz_cglx").text().indexOf('直达到货') != -1) {
        column = [
            [
                {field: 'wzgg.oldcode', title: '老物资编码', width: 30},
                {field: 'wzgg.code', title: '新物资编码', width: 30},
                {field: 'wzgg.name', title: '物资名称', width: 20},
                {field: 'wzgg.ggxh', title: '规格型号', width: 20},
                {field: 'wzgg.dw', title: '单位', width: 10},
                {field: 'ck', title: '长宽', width: 20},
                {field: 'cd.z', title: '产地', width: 30},
                {field: 'jzfs.z', title: '记重方式', width: 25},
                {field: 'js', title: '件数',
                    formatter: function (value) {
                        var sl = formatSl(value, ",");
                        return sl;
                    }, width: 40},
                {field: 'sbsl', title: '数量',
                    formatter: function (value) {
                        var sl = formatSl(value, ",");
                        return sl;
                    }, width: 50},
                {field: 'hsj', title: '含税单价(元)',
                    formatter: function (value) {
                        var sl = formatJe(value, ",");
                        return sl;
                    }, width: 50},
                {field: 'bhsj', title: '不含税单价(元)',
                    formatter: function (value) {
                        var sl = formatJe(value, ",");
                        return sl;
                    }, width: 50}
            ]
        ]
    } else {
        column = [
            [
                {field: 'wzgg.oldcode', title: '老物资编码', width: 30},
                {field: 'wzgg.code', title: '新物资编码', width: 30},
                {field: 'wzgg.name', title: '物资名称', width: 20},
                {field: 'wzgg.ggxh', title: '规格型号', width: 20},
                {field: 'wzgg.dw', title: '单位', width: 10},
                {field: 'ck', title: '长宽', width: 20},
                {field: 'cd.z', title: '产地', width: 30},
                {field: 'jzfs.z', title: '记重方式', width: 25},
                {field: 'js', title: '件数',
                    formatter: function (value) {
                        var sl = formatSl(value, ",");
                        return sl;
                    }, width: 40},
                {field: 'sbsl', editor: {type: 'numberbox', options: {min: 0, precision: 4}}, title: '数量',
                    formatter: function (value) {
                        var sl = formatSl(value, ",");
                        return sl;
                    }, width: 50},
                {field: 'hsj', title: '含税单价(元)',
                    formatter: function (value) {
                        var sl = formatJe(value, ",");
                        return sl;
                    }, width: 50},
                {field: 'bhsj', title: '不含税单价(元)',
                    formatter: function (value) {
                        var sl = formatJe(value, ",");
                        return sl;
                    }, width: 50}
            ]
        ]
    }


    $("#cgdtz_mx").edatagrid({
        url: href,
        fitColumns: true,
        singleSelect: true,
        fit: true,
        checkOnSelect: false,
        selectOnCheck: false,
        showFooter: true,
        rownumbers: true,
        columns: column,
        loadFilter: function (data) {
            $.each(data.rows, function () {
                this.sbsl = 0;
            })
            return data;
        },
        onSelect: function (rowIndex, rowData) {
            if ($("#cgdtz_cglx").text().indexOf('直达到货') == -1) {
                //获取每个单元格
                $("#cgdtz_mx").edatagrid('editRow', rowIndex);
                var editors = $("#cgdtz_mx").datagrid('getEditors', rowIndex);
                if (editors.length == 0)
                    return;
            } else {
                return;
            }
        },
        onLoadSuccess: function () {
            countSumCgdmx();
        }
    })
}


function fillFy(href) {
    $("#cgdtz_fy").edatagrid({
        url: href,
        fitColumns: true,
        singleSelect: true,
        fit: true,
        checkOnSelect: false,
        selectOnCheck: false,
        rownumbers: true,
        columns: [
            [
                {field: 'sydw.qc', title: '收益单位', width: 40},
                {field: 'fylb.fykmjc', title: '费用类别', width: 17},
                {field: 'jfsl', title: '计费数量',
                    formatter: function (value) {
                        var sl = formatSl(value, ",");
                        return sl;
                    }, width: 40},
                {field: 'dj', title: '单价',
                    formatter: function (value) {
                        var sl = formatJe(value, ",");
                        return sl;
                    }, width: 50},
                {field: 'je', title: '总金额',
                    formatter: function (value) {
                        var sl = formatJe(value, ",");
                        return sl;
                    }, width: 50},
                {field: 'bz', title: '备注', width: 30}
            ]
        ]
    })
}


//采购单明细合计
function countSumCgdmx() {
    var sumSbsl = 0;
    var sumHsj = 0;
    var sumBhsj = 0;
    var data = $("#cgdtz_mx").edatagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var sbsl = parseFloat(data.rows[i]["sbsl"]);
        var hsj = parseFloat(data.rows[i]["hsj"]);
        var bhsj = parseFloat(data.rows[i]["bhsj"]);
        sbsl = isNaN(sbsl) ? 0 : sbsl;
        hsj = isNaN(hsj) ? 0 : hsj;
        bhsj = isNaN(bhsj) ? 0 : bhsj;
        sumSbsl += sbsl;
        sumHsj += hsj;
        sumBhsj += bhsj;
    }
    addFooter(sumSbsl, sumHsj, sumBhsj);
}

function addFooter(sumSbsl, sumHsj, sumBhsj) {
    var footer = null;
    footer = [
        {"wzgg.code": "合计", "sbsl": formatMoneyFourWei(sumSbsl), "hsj": formatMoneyFourWei(sumHsj), "bhsj": formatMoneyFourWei(sumBhsj)}
    ];
    if (footer != null)
        $("#cgdtz_mx").edatagrid('reloadFooter', footer);
}


